/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.Folder.FolderOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.CreateGalSyncAccountRequest;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import com.zimbra.soap.admin.type.GalMode;
/*     */ import com.zimbra.soap.type.AccountBy;
/*     */ import com.zimbra.soap.type.AccountSelector;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateGalSyncAccount
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public boolean domainAuthSufficient(Map<String, Object> context)
/*     */   {
/*  60 */     return true;
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  66 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  67 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  69 */     CreateGalSyncAccountRequest cgaRequest = (CreateGalSyncAccountRequest)JaxbUtil.elementToJaxb(request);
/*     */     
/*  71 */     String name = cgaRequest.getName();
/*  72 */     String domainStr = cgaRequest.getDomain();
/*  73 */     GalMode type = cgaRequest.getType();
/*     */     
/*  75 */     AccountSelector acctSelector = cgaRequest.getAccount();
/*  76 */     AccountBy acctBy = acctSelector.getBy();
/*  77 */     String acctValue = acctSelector.getKey();
/*     */     
/*  79 */     String password = cgaRequest.getPassword();
/*  80 */     String folder = cgaRequest.getFolder();
/*     */     
/*  82 */     String mailHost = cgaRequest.getMailHost();
/*     */     
/*  84 */     Domain domain = prov.getDomainByName(domainStr);
/*     */     
/*  86 */     if (domain == null) {
/*  87 */       throw AccountServiceException.NO_SUCH_DOMAIN(domainStr);
/*     */     }
/*     */     
/*  90 */     Account account = null;
/*     */     try {
/*  92 */       account = prov.get(acctBy.toKeyAccountBy(), acctValue, zsc.getAuthToken());
/*     */     } catch (ServiceException se) {
/*  94 */       ZimbraLog.gal.warn("error checking GalSyncAccount", se);
/*     */     }
/*     */     
/*     */ 
/*  98 */     if (account == null) {
/*  99 */       if (acctBy != AccountBy.name) {
/* 100 */         throw AccountServiceException.NO_SUCH_ACCOUNT(acctValue);
/*     */       }
/*     */       
/* 103 */       for (String acctId : domain.getGalAccountId()) {
/* 104 */         Account acct = prov.getAccountById(acctId);
/* 105 */         if ((acct != null) && (acct.getMailHost().equals(mailHost))) {
/* 106 */           throw AccountServiceException.ACCOUNT_EXISTS(acct.getName());
/*     */         }
/*     */       }
/*     */       
/* 110 */       checkDomainRightByEmail(zsc, acctValue, Rights.Admin.R_createAccount);
/* 111 */       Map<String, Object> accountAttrs = new HashMap();
/* 112 */       StringUtil.addToMultiMap(accountAttrs, "zimbraIsSystemResource", "TRUE");
/* 113 */       StringUtil.addToMultiMap(accountAttrs, "zimbraIsSystemAccount", "TRUE");
/* 114 */       StringUtil.addToMultiMap(accountAttrs, "zimbraHideInGal", "TRUE");
/* 115 */       StringUtil.addToMultiMap(accountAttrs, "zimbraContactMaxNumEntries", "0");
/* 116 */       StringUtil.addToMultiMap(accountAttrs, "zimbraMailHost", mailHost);
/* 117 */       checkSetAttrsOnCreate(zsc, TargetType.account, acctValue, accountAttrs);
/* 118 */       account = prov.createAccount(acctValue, password, accountAttrs);
/*     */     }
/*     */     
/* 121 */     if (!Provisioning.onLocalServer(account)) {
/* 122 */       String host = account.getMailHost();
/* 123 */       Server server = prov.getServerByName(host);
/* 124 */       return proxyRequest(request, context, server);
/*     */     }
/* 126 */     addDataSource(request, zsc, account, domain, folder, name, type);
/*     */     
/* 128 */     Element response = zsc.createElement(AdminConstants.CREATE_GAL_SYNC_ACCOUNT_RESPONSE);
/* 129 */     ToXML.encodeAccount(response, account, false, emptySet, null);
/* 130 */     return response;
/*     */   }
/*     */   
/*     */   static void addDataSource(Element request, ZimbraSoapContext zsc, Account account, Domain domain, String folder, String name, GalMode type) throws ServiceException
/*     */   {
/* 135 */     String acctName = account.getName();
/* 136 */     String acctId = account.getId();
/* 137 */     HashSet<String> galAcctIds = new HashSet();
/* 138 */     galAcctIds.addAll(Arrays.asList(domain.getGalAccountId()));
/* 139 */     if (!galAcctIds.contains(acctId)) {
/* 140 */       galAcctIds.add(acctId);
/* 141 */       domain.setGalAccountId((String[])galAcctIds.toArray(new String[0]));
/*     */     }
/*     */     
/*     */ 
/* 145 */     if (folder == null) {
/* 146 */       folder = "/Contacts";
/* 147 */     } else if ((folder.length() > 0) && (folder.charAt(0) != '/')) {
/* 148 */       folder = "/" + folder;
/*     */     }
/* 150 */     Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 151 */     Folder contactFolder = null;
/*     */     try {
/* 153 */       contactFolder = mbox.getFolderByPath(null, folder);
/*     */     } catch (MailServiceException.NoSuchItemException e) {
/* 155 */       contactFolder = mbox.createFolder(null, folder, new Folder.FolderOptions().setDefaultView(MailItem.Type.CONTACT));
/*     */     }
/*     */     
/* 158 */     int folderId = contactFolder.getId();
/*     */     
/*     */ 
/* 161 */     for (DataSource ds : account.getAllDataSources()) {
/* 162 */       if (ds.getFolderId() == folderId) {
/* 163 */         throw MailServiceException.ALREADY_EXISTS("data source " + ds.getName() + " already contains folder " + folder);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 168 */     mbox.grantAccess(null, folderId, domain.getId(), (byte)4, ACL.stringToRights("r"), null);
/*     */     
/*     */ 
/* 171 */     Map<String, Object> attrs = AdminService.getAttrs(request, true);
/*     */     try {
/* 173 */       attrs.put("zimbraGalType", type.name());
/* 174 */       attrs.put("zimbraDataSourceFolderId", "" + folderId);
/* 175 */       if (!attrs.containsKey("zimbraDataSourceEnabled")) {
/* 176 */         attrs.put("zimbraDataSourceEnabled", "TRUE");
/*     */       }
/* 178 */       if (!attrs.containsKey("zimbraGalStatus")) {
/* 179 */         attrs.put("zimbraGalStatus", "enabled");
/*     */       }
/* 181 */       Provisioning.getInstance().createDataSource(account, DataSourceType.gal, name, attrs);
/*     */     } catch (ServiceException e) {
/* 183 */       ZimbraLog.gal.error("error creating datasource for GalSyncAccount", e);
/* 184 */       throw e;
/*     */     }
/*     */     
/* 187 */     ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "CreateGalSyncAccount", "name", acctName }));
/*     */   }
/*     */   
/* 190 */   private static final Set<String> emptySet = ;
/*     */   
/*     */ 
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 195 */     relatedRights.add(Rights.Admin.R_createAccount);
/* 196 */     notes.add(String.format("All attrs provided in the attribute list have to settable by. the authed admin.   You can grant the %s right, which allows setting all attributes on %s, or grant the set attrs right just for the attributes the admin needs to set while creating an entry.", new Object[] { Rights.Admin.R_modifyAccount.getName(), "account" }));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/CreateGalSyncAccount.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */